<?php

/**
 * The form for inputting a new phase into the database.
 * @author Andy Rich <andy.rich@gmail.com>
 * @version 1.0
 * @since 2013-11-02
 */
require_once 'database.php';
require_once 'phase.php';
require_once 'schedule.php';

loginInfo::update_login ();
$db = new database ();
$order_id = $_REQUEST["order_id"];

$machines = $db->query("SELECT id, name FROM Machines");
$materials = $db->query ("SELECT id, name FROM Materials");

?><html>
	<head>
		<title>Add new phase - Opol</title>
		<style type="text/css">
			label{
				display: inline-block;
				float: left;
				clear: left;
				width: 80px;
				text-align: right;
				padding-right: 10px;
			}
			input, select, textarea {
				display: inline-block;
				float: left;
				margin-bottom: 10px;
			}
		</style>
	</head>
	<body>
		<link rel="stylesheet" type="text/css" href="opol.css">
		<?php require 'menu.php'; ?>
		<br>
<?php
if ($_POST) {

	$order = new order ((int)$order_id);
	$order->init ($db);
	$priority = $order->num_phases ($db);

	$mats_qts = $_POST['mats_qt'];
	$mats = array ();
	foreach ($mats_qts as $id => $qty) {
		if ((int)$qty > 0)
			$mats[$id] = (int)$qty;
	}

	$mach = $_POST["mach_id"];
	$keyword = $_POST['keyword'];
	$time = (double)phase::calculate_approx_time ($db, $keyword);
	if ($time == -1) {
		echo "You do not have sufficient permissions to perform this action.<br>\n";
	} else {
		// Use order quantity.
		$time *= (double)$order->quantity;

		$new_phase = $db->prepare("INSERT INTO Phases VALUES (NULL, ?, ?, ?, ?, ?, 0, NULL, NULL)");

		$new_phase->bind_param("iissi", $mach, $order_id, $time, $keyword, $priority);

		$new_phase->execute();

		$phase_number = $db->insert_id();
		if ($phase_number == 0) {
			print "You do not have sufficient permissions to perform this action.<br>\n";
		} else {
			$obj = new phase ((int)$phase_number);
			$obj->init ($db);
			$obj->check_materials_with_arr ($db, $mats);

			schedule::do_sched ($db);

			print "<h2>Phase added!</h2>";
			print "Enter the next phase below, or <a href='/'>return to the main menu</a>";
		}
	}
}
?>
		<h1>Add a new phase</h1>
		<form name="text" action="new_phase.php" method="post">
		<input type="hidden" name="order_id" value="<? print $order_id;?>">

			<label>Machine: </label> 
				<Select name="mach_id">
<?php
while ($row = $machines->fetch_assoc()){
	$id = $row['id']; $name = $row['name'];
	print "\t\t\t\t\t<option value='$id'>$name</option>\n";
}

?>				</select> <br clear="both">
			<label>Keyword: </label>
				<input name="keyword" type="text"><br clear="both">
				<label>Materials: </label><br clear="both">
				<table id='input_table'>
					<tr>
						<th> Name </th>
						<th> Quantity </th>
					</tr>
<?php
				while ($row = $materials->fetch_assoc()) {
					$id = $row['id']; $name = $row['name'];
					print "\t\t\t\t\t<tr>\n";
					print "\t\t\t\t\t\t<td>$name</td>\n";
					print "\t\t\t\t\t\t<td><input type='text' name='mats_qt[$id]'></input></td>\n";
					print "\t\t\t\t\t</tr>\n";
				}
?>
				</table>
			<br clear="both">
				<label>&nbsp;</label>
				<input type="submit" value="Save"> <br>
		</form>
	</body>
</html>
  
